<?php
/* author: ovsexia
 * version: 2.0.0
 * name: Words2img
 * describe: 文本转图片
 * License: Mozilla Public License Version 2.0
 */
namespace ovsexia;

class Words2img
{
	/* 文字生成图片
	 * $words       文本
	 * $wordstype   语言类型[cn汉字, en拉丁文]
	 * $size        字体大小
	 * $wordswidth  每个文字宽度
	 * $width       画布宽度
	 * $height      画布高度
	 * $padding     边距
	 * $lineheight  行高
	 * $color       文字颜色 array(0, 0, 0)
	 * $bgcolor     背景颜色 array(255, 255, 255)
	 */
	function __construct($words='', $wordstype='cn', $size=12, $wordswidth=12, $width=100, $height=100, $padding=10, $lineheight=30, $color=array(255, 255, 255), $bgcolor=array(0, 0, 0))
	{
		$this->words = $words;
		$this->wordstype = $wordstype;
		$this->size = $size;
		$this->wordswidth = $wordswidth;
		$this->width = $width;
		$this->height = $height;
		$this->padding = $padding;
		$this->lineheight = $lineheight;
		$this->color = $color;
		$this->bgcolor = $bgcolor;

		$this->wordsp = array();
	}

	//保存路径
	function savedir($dir='data/')
	{
		$this->md5dir = $dir;
	}

	function img($show=false)
	{
		if($show===false){
			if($this->md5dir){
				$md5dir = $this->md5dir;
			}else{
				$md5dir = 'data/';
			}
			if(!file_exists($md5dir)){
				$this->fsMake($md5dir);
			}
			$md5 = serialize($this->words).'_'.$this->size.'_'.$this->wordswidth.'_'.$this->width.'_'.$this->height.'_'.$this->padding.'_'.$this->lineheight;
			$md5file = $md5dir.md5($md5).'.png';
		}

		$realwidth = $this->width - ($this->padding * 2);  //文字区域宽度

		//文字处理
		if(empty($this->words)){
			return false;
		}
		if(!is_array($this->words)){  //数组
			$words= $this->words;
			$this->words = array($words);
		}
		$height = 0;
		foreach($this->words as $key=>$value)
		{
			if(is_array($value)){
				if(file_exists($value[0])){
					$this->wordsp[] = $this->imgpara($value);
				}
			}else{
				if($this->wordstype=='cn'){  //汉字
					$this->wordsarr = $this->str_array($value);  //文字打散
					$this->wordsline_cn($realwidth);
				}else{  //拉丁文
					$this->wordsarr = $this->word_array($value);  //单词打散
					$this->wordsline_en($realwidth);
				}
			}
		}

		foreach($this->wordsp as $key=>$value)
		{
			$first = reset($value);
			$next = next($value);
			if(count($value)==7 && file_exists($next) && in_array($first, array('jpg', 'png', 'gif'))){
				$height += $value['newheight'] + intval($this->lineheight / 2);
			}else{
				$height += $this->lineheight;
			}
			unset($first, $next);
		}
		$height += $this->padding * 2;

		if($this->height!='auto'){  //自动高度
			$height = $this->height;
		}

		//生成图片
		$this->img = imagecreatetruecolor($this->width, $height);
		$color = imagecolorallocate($this->img, $this->color[0], $this->color[1], $this->color[2]);  //字体颜色
		$bgcolor = imagecolorallocate($this->img, $this->bgcolor[0], $this->bgcolor[1], $this->bgcolor[2]);  //背景颜色
		$fontdir = __DIR__;
		$font = $fontdir.'\SourceHanSansCN-Normal.otf';
		$x = $this->padding;
		$y = -8 + $this->padding;
		imagefill($this->img, 0, 0, $bgcolor);

		foreach($this->wordsp as $key=>$value)
		{
			$first = reset($value);
			$next = next($value);
			if(count($value)==7 && file_exists($next) && in_array($first, array('jpg', 'png', 'gif'))){
				if($value['filetype']=='jpg'){
					$inimg = imagecreatefromjpeg($value['file']);
				}elseif($value['filetype']=='png'){
					$inimg = imagecreatefrompng($value['file']);
				}elseif($value['filetype']=='gif'){
					$inimg = imagecreatefromgif($value['file']);
				}
				$y += intval($this->lineheight / 2);
				imagecopyresized($this->img, $inimg, $value['x'], $y, 0, 0, $value['newwidth'], $value['newheight'], $value['width'], $value['height']);
				$y += $value['newheight'];
			}else{
				$string = implode('', $value);
				$y += $this->lineheight;
				imagettftext($this->img, $this->size, 0, $x, $y, $color, $font, $string);
			}
			unset($first, $next);
		}

		//输出
		if($show===true){
			imagepng($this->img);
			imagedestroy($this->img);
		}else{
			imagepng($this->img, $md5file);
			imagedestroy($this->img);
			return $md5file;
		}
	}

	//直接显示图片
	function imgshow()
	{
		header('Content-type:image/png');
		$this->img(true);
	}

	//图片参数处理
	function imgpara($img=array())
	{
		$array = explode('.', $img[0]);
		$filetype = strtolower(end($array));
		$result['filetype'] = $filetype==='jpeg' ? 'jpg' : $filetype;
		$result['file'] = $img[0];
		$getimagesize = getimagesize($img[0]);
		$result['width'] = $getimagesize[0];
		$result['height'] = $getimagesize[1];

		preg_match('/(^[\d\.]+)%$/U', $img[1], $match);
		if($match && $match[1]){  //百分比
			$result['newwidth'] = ($this->width - ($this->padding * 2)) * $match[1] * 0.01;
		}else{
			$result['newwidth'] = $img[1];
		}

		if($img[2]=='auto'){  //自适应
			$per = $result['newwidth'] / $result['width'];
			$result['newheight'] = intval($per * $result['height']);
		}else{
			$result['newheight'] = intval($img[2]);
		}
		if(!isset($img[3])){
			$img[3] = 'left';
		}
		if($img[3]=='center' || $img[3]=='right'){
			$bodywidth = $this->width - ($this->padding * 2);
			if($result['newheight'] < $bodywidth){
				if($img[3]=='center'){
					$result['x'] = (($bodywidth - $result['newwidth']) / 2) + $this->padding;
				}else{
					$result['x'] = $bodywidth - $result['newwidth'] + $this->padding;
				}
			}else{
				$result['x'] = $this->padding;
			}
		}else{
			$result['x'] = $this->padding;
		}
		return $result;
	}

	//文字分行
	function wordsline_cn($width=600)
	{
		if($this->wordsarr){
			$w = 0;
			foreach($this->wordsarr as $key=>$value)
			{
				$data[] = $value;
				if($this->ifwordscn($value)){
					$w += $this->wordswidth;
				}else{
					$w += $this->wordswidth / 2;
				}
				if($w > $width){  //一行满
					$this->wordsp[] = $data;
					$this->wordsarr = array_slice($this->wordsarr, $key+1);
					return $this->wordsline_cn($width);
				}
			}
			$this->wordsp[] = $data;
		}else{
			return false;
		}
	}

	//单词分行
	function wordsline_en($width=600)
	{
		if($this->wordsarr){
			$w = 0;
			foreach($this->wordsarr as $key=>$value)
			{
				$data[] = $value.' ';
				$w += mb_strlen($value) * $this->wordswidth;
				if($w > $width){  //一行满
					$this->wordsp[] = $data;
					$this->wordsarr = array_slice($this->wordsarr, $key+1);
					return $this->wordsline_en($width);
				}
			}
			$this->wordsp[] = $data;
		}else{
			return false;
		}
	}

	//文字类型判断
	function ifwordscn($words='')
	{
		$reg = '/[\x81-\xfe]./';
		if(preg_match($reg, $words)){
			return true;  //含有全角字符
		}else{
			return false;
		}
	}

	//单个字符转数组
	function str_array($string)
	{
	    $strlen = mb_strlen($string);
	    while ($strlen) {
	        $array[] = mb_substr($string, 0, 1, 'utf8');
	        $string = mb_substr($string, 1, $strlen, 'utf8');
	        $strlen = mb_strlen($string);
	    }
	    return $array;
	}

	//单词转数组
	function word_array($string)
	{
		$array = explode(' ', $string);
		return $array;
	}

	//创建文件或目录
	function fsMake($file, $type='dir')
	{
		$array = explode('/', $file);
		$count = count($array);
		$msg = '';
		if($type=='dir'){
			for($i=0;$i<$count;$i++){
				$msg .= $array[$i];
				if(!file_exists($msg) && ($array[$i])){
					mkdir($msg, 0777);
				}
				$msg .= '/';
			}
		}else{
			for($i=0;$i<($count-1);$i++){
				$msg .= $array[$i];
				if(!file_exists($msg) && ($array[$i])){
					mkdir($msg, 0777);
				}
				$msg .= '/';
			}
			@touch($file);//创建文件
		}
		return true;
	}
}
?>